package test;

import java.util.Stack;

public class Minstack {
    //力扣 最小栈
    private Stack<Integer> stack;
    private Stack<Integer> minstack;

    public Minstack() {
        this.stack = new Stack<Integer>();
        this.minstack = new Stack<Integer>();

    }

    public void push(int val) {
        stack.push(val);
        if (minstack.isEmpty()) {
            minstack.push(val);
        } else {
            //这里一定是要小于等于
            if (minstack.peek() >= val) {
                minstack.push(val);
            }
        }
    }

    public void pop() {
        if (!stack.isEmpty()) {
            int ret = stack.pop();
            if (ret == minstack.peek()) {
                minstack.pop();
            }
        }

    }

    public int top() {
        if(stack.isEmpty()){
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        if (minstack.isEmpty()) {
            return -1;
        }
        return minstack.peek();
    }

}
